# 社区服务平台开机自启动配置脚本
# 需要以管理员权限运行

Write-Host "========================================" -ForegroundColor Green
Write-Host "社区服务平台开机自启动配置" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Green
Write-Host ""

# 检查管理员权限
if (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
    Write-Host "❌ 请以管理员权限运行此脚本！" -ForegroundColor Red
    Write-Host "右键点击PowerShell，选择'以管理员身份运行'" -ForegroundColor Yellow
    pause
    exit
}

Write-Host "✅ 管理员权限确认" -ForegroundColor Green
Write-Host ""

# 获取当前脚本目录
$scriptPath = Split-Path -Parent $MyInvocation.MyCommand.Path
$startupScript = Join-Path $scriptPath "windows-autostart.bat"

Write-Host "🔧 正在创建Windows任务计划..." -ForegroundColor Yellow

# 创建任务计划
$taskName = "CommunityServicePlatform"
$taskDescription = "社区服务平台开机自启动"

# 删除已存在的任务
try {
    Unregister-ScheduledTask -TaskName $taskName -Confirm:$false -ErrorAction SilentlyContinue
    Write-Host "✅ 清理旧任务完成" -ForegroundColor Green
} catch {
    Write-Host "ℹ️  没有找到旧任务" -ForegroundColor Blue
}

# 创建新任务
$action = New-ScheduledTaskAction -Execute $startupScript -WorkingDirectory $scriptPath
$trigger = New-ScheduledTaskTrigger -AtStartup
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable -RunLevel Highest

try {
    Register-ScheduledTask -TaskName $taskName -Action $action -Trigger $trigger -Settings $settings -Description $taskDescription -User "SYSTEM"
    Write-Host "✅ Windows任务计划创建成功！" -ForegroundColor Green
} catch {
    Write-Host "❌ 任务计划创建失败: $($_.Exception.Message)" -ForegroundColor Red
    Write-Host "请手动创建任务计划:" -ForegroundColor Yellow
    Write-Host "1. 打开'任务计划程序'" -ForegroundColor Yellow
    Write-Host "2. 创建基本任务" -ForegroundColor Yellow
    Write-Host "3. 名称: CommunityServicePlatform" -ForegroundColor Yellow
    Write-Host "4. 触发器: 计算机启动时" -ForegroundColor Yellow
    Write-Host "5. 操作: 启动程序" -ForegroundColor Yellow
    Write-Host "6. 程序: $startupScript" -ForegroundColor Yellow
}

Write-Host ""
Write-Host "========================================" -ForegroundColor Green
Write-Host "配置完成！" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Green
Write-Host ""
Write-Host "📋 配置信息:" -ForegroundColor Cyan
Write-Host "   任务名称: $taskName" -ForegroundColor White
Write-Host "   启动脚本: $startupScript" -ForegroundColor White
Write-Host "   触发条件: 系统启动时" -ForegroundColor White
Write-Host ""
Write-Host "🔧 管理命令:" -ForegroundColor Cyan
Write-Host "   查看任务: Get-ScheduledTask -TaskName '$taskName'" -ForegroundColor White
Write-Host "   删除任务: Unregister-ScheduledTask -TaskName '$taskName'" -ForegroundColor White
Write-Host "   手动运行: Start-ScheduledTask -TaskName '$taskName'" -ForegroundColor White
Write-Host ""
Write-Host "⚠️  重要提醒:" -ForegroundColor Yellow
Write-Host "   1. 确保MySQL服务路径正确" -ForegroundColor White
Write-Host "   2. 确保Node.js和PM2已安装" -ForegroundColor White
Write-Host "   3. 重启计算机测试自启动功能" -ForegroundColor White
Write-Host "========================================" -ForegroundColor Green

pause
